package com.dinggou.shopjb.activitys;

import android.app.Dialog;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.dinggou.shopjb.R;
import com.dinggou.shopjb.adapters.IntegralRecordAdapter;
import com.dinggou.shopjb.application.MyApplication;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.IntegralRecordBean;
import com.dinggou.shopjb.bean.UserInfoBean;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.DataUtils;
import com.dinggou.shopjb.utils.ToastUtil;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class IntegralRecordActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "IntegralRecordActivity";
    private RadioButton rb_take,rb_use;
    private TextView integral_number,select_time;
    private SmartRefreshLayout refresh_layout;
    private int page = 1;
    private String  integral_type = "1"; //1获取  2 使用
    private String  mouth;
    private RecyclerView recyclerView;
    private IntegralRecordAdapter adapter;
    private List<IntegralRecordBean> list = new ArrayList();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM", Locale.CHINA);

    @Override
    protected int setContentView() {
        return R.layout.activity_integral_record;
    }

    @Override
    protected void findViews() {
        integral_number = $(R.id.integral_number);
        select_time = $(R.id.select_time);
        rb_take = $(R.id.rb_take);
        rb_use = $(R.id.rb_use);
        refresh_layout = $(R.id.refresh_layout);
        recyclerView =  $(R.id.rv_integral_record);
        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new IntegralRecordAdapter(mContext,list);
        recyclerView.setAdapter(adapter);

        rb_take.setChecked(true);
        UserInfoBean userInfo = DataUtils.getUserInfo(mContext);
        integral_number.setText(userInfo.integral+"");
        mouth =  dateFormat.format(new Date(System.currentTimeMillis()));
        select_time.setText(mouth);
        getListDate();
    }

    @Override
    protected void setListeners() {
        rb_take.setOnClickListener(this);
        rb_use.setOnClickListener(this);
        select_time.setOnClickListener(this);
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getListDate();

            }
        });

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getListDate();
            }
        });

    }

    private void getListDate(){
        refresh_layout.finishRefresh(2000);
        refresh_layout.finishLoadMore(2000);
        mAppAction =  MyApplication.getApplication().getAppAction();
        mAppAction. getIntegralLog(integral_type, mouth, page, new ActionCallbackListener<List<IntegralRecordBean>>() {
            @Override
            public void onSuccess(List<IntegralRecordBean> data) {
                Log.e(TAG, "onSuccess: "+data.toString());
                if(page == 1){
                    list.clear();
                }
                list.addAll(data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });

    }
    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.rb_take){
            integral_type ="1";
            page = 1;
            getListDate();
            rb_take.setChecked(true);
            rb_use.setChecked(false);
         return;
        }
        if(view.getId() == R.id.rb_use){
            integral_type ="2";
            page = 1;
            getListDate();
            rb_take.setChecked(false);
            rb_use.setChecked(true);
            return;
        }
        if(view.getId() == R.id.select_time){
            showDateDialog();
            return;
        }
    }

    private void showDateDialog() {
        try {
            Calendar c = Calendar.getInstance();
            View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date_picker, null);
            final Dialog mOneDrawDialog = new Dialog(mContext, R.style.dialog);
            mOneDrawDialog.setContentView(view);
            mOneDrawDialog.setCanceledOnTouchOutside(false);
            mOneDrawDialog.setCancelable(false);
            Window window = mOneDrawDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            final DatePicker datePicker = view.findViewById(R.id.date_picker);
            datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), null);
            int i = c.get(Calendar.YEAR);
            String maxDate = i + "-12-31";
            Date parse = dateFormat.parse(maxDate);
            hideDay(datePicker);
            datePicker.setMaxDate(parse.getTime());
            TextView tv_cancel = view.findViewById(R.id.tv_cancel);
            TextView tv_confirm = view.findViewById(R.id.tv_confirm);
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOneDrawDialog.dismiss();
                }
            });
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    page = 1;
                    String month = datePicker.getMonth() + 1 < 10 ? "0"+(datePicker.getMonth() + 1): String.valueOf(datePicker.getMonth() + 1);
                    mouth = datePicker.getYear() + "-" + month;
                    select_time.setText(mouth);
                    getListDate();
                    mOneDrawDialog.dismiss();
                }
            });
            if (!mOneDrawDialog.isShowing()) {
                mOneDrawDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void hideDay(DatePicker mDatePicker) {
        try {
            /* 处理android5.0以上的特殊情况 */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
                if (daySpinnerId != 0) {
                    View daySpinner = mDatePicker.findViewById(daySpinnerId);
                    if (daySpinner != null) {
                        daySpinner.setVisibility(View.GONE);
                    }
                }
            } else {
                Field[] datePickerFields = mDatePicker.getClass().getDeclaredFields();
                for (Field datePickerField : datePickerFields) {
                    if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = new Object();
                        try {
                            dayPicker = datePickerField.get(mDatePicker);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
